<?php

/**
 * Spectacle form.
 *
 * @package    tfe
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class SpectacleForm extends BaseSpectacleForm
{
  private static $intervalle_jour_seance = 1;

  public function configure()
  {
      unset($this['created_at'], $this['updated_at'], $this['deleted_at']);

      $this->widgetSchema['commentaire'] =  new sfWidgetFormCKEditor(array('jsoptions'=>array('toolbar'=>'Full','skin'=>'kama', 'height'=>'300px')));

      $this->widgetSchema['metteur_en_scene_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Artiste', 'order_by' => array('nom','asc')));
      $this->widgetSchema['metteur_en_scene_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
      $this->widgetSchema['metteur_en_scene_list']->setOption('renderer_options', array(
          'label_associated' => 'Selectionnés',
          'label_unassociated' => 'Non sélectionnés'
      ));

      $this->widgetSchema['acteurs_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Artiste', 'order_by' => array('nom','asc')));
      $this->widgetSchema['acteurs_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
      $this->widgetSchema['acteurs_list']->setOption('renderer_options', array(
          'label_associated' => 'Sélectionnés',
          'label_unassociated' => 'Non sélectionnés'
      ));

      $this->widgetSchema['intervenants_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Artiste', 'order_by' => array('nom','asc')));
      $this->widgetSchema['intervenants_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
      $this->widgetSchema['intervenants_list']->setOption('renderer_options', array(
          'label_associated' => '',
          'label_unassociated' => ''
      ));

      $this->widgetSchema['abonnements_list'] = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Abonnement', 'order_by' => array('libelle','asc')));
      $this->widgetSchema['abonnements_list']->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
      $this->widgetSchema['abonnements_list']->setOption('renderer_options', array(
          'label_associated' => '',
          'label_unassociated' => ''
      ));


      $dateWidget = new sfWidgetFormI18nDate(array(
        'format' => '%day%/%month%/%year%',
        'culture'=>'fr'
        ));
      $this->widgetSchema['date_de_debut'] = new sfWidgetFormJqueryDate(array(
          'date_widget' => $dateWidget,
         'culture'=>'fr_FR'
        ));

      $dateWidget = new sfWidgetFormI18nDate(array(
        'format' => '%day%/%month%/%year%',
        'culture'=>'fr'
        ));
      $this->widgetSchema['date_de_fin'] = new sfWidgetFormJqueryDate(array(
          'date_widget' => $dateWidget,
         'culture'=>'fr_FR'
        ));

      $this->validatorSchema->setPostValidator(
              new sfValidatorSchemaCompare('date_de_debut', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'date_de_fin',
              array(),
              array('invalid' => 'La date de début doit précéder la date de fin !')
          )
      );
      
  }


     public function secondes_to_duree($secondes){
	$s=$secondes % 60; //reste de la division en minutes => secondes
	$m1=($secondes-$s) / 60; //minutes totales
	$m=$m1 % 60;//reste de la division en heures => minutes
	$h=($m1-$m) / 60; //heures
	$resultat=$h.":".$m.":".$s;
	return $resultat;
    }

    public function duree_to_secondes($duree){
	$array_duree=explode(":",$duree);
	$secondes=3600*$array_duree[0]+60*$array_duree[1]+$array_duree[2];
	return $secondes;
    }


  public function save($con = null)
  {
        $spectacle = parent::save();

        // champs mandatory : libelle, date_de_debut, date_de_fin
                $dateDebut = $spectacle->getDateDeDebut();
                $dateFin   = $spectacle->getDateDeFin();

        if (!is_null($spectacle) && !empty($dateDebut)&& !empty($dateFin)) {
            // supprimer ce qui a deja ete insere dans la table EVENEMENT
            EvenementTable::supprimerRepresentationSpectacle($spectacle->getId(), date('Y-m-d'));

                // format timestamp pour les comparaisons
                $dateSeance    = strtotime($dateDebut);
                $dateSeanceFin = strtotime($dateFin);

                // tant que la date de seance n'est superieur a la date de fin

                while ($dateSeance <= $dateSeanceFin) {
                    // format de la date de la seance
                    $dateSeance = date('Y-m-d',$dateSeance);
                    //Calcul de l'heure de fin
                    $heureDeFin = $this->duree_to_secondes($spectacle->getDuree());
                    $DebutEnSecondes = $this->duree_to_secondes($spectacle->getHeureDeDebut());
                    $heureDeFin = $heureDeFin + $DebutEnSecondes;
                    $heureDeFin = $this->secondes_to_duree($heureDeFin);

                    // placer les differents attributs
                    $RepresentationSpectacle = new RepresentationSpectacle();
                    $RepresentationSpectacle->setIdSpectacle($spectacle->getId());
                    $RepresentationSpectacle->setLibelle($spectacle->getTitre());
                    $RepresentationSpectacle->setDate($dateSeance);
                    $RepresentationSpectacle->setHeureDeDebut($spectacle->getHeureDeDebut());
                    $RepresentationSpectacle->setHeureDeFin($heureDeFin);
                    $RepresentationSpectacle->setLieu($spectacle->getLieu());
                    $RepresentationSpectacle->setCommentaire($spectacle->getCommentaire());
                    $RepresentationSpectacle->setType('spectacle');

                    // insertion de la seance
                    $RepresentationSpectacle->save();

                    // ajout de l'intervalle a la date de la seance courante
                    list($year, $month, $day) = explode('-', $dateSeance);
                    $timestamp  = mktime(0,0,0, $month, $day + self::$intervalle_jour_seance, $year);
                    $dateSeance = date('Y-m-d',$timestamp);
                    $dateSeance = strtotime($dateSeance);
            }
        }
        return $spectacle;
  }





}
